/*******************************************************************************
 * SVN­Version­Information:
 * ­­­­­­­­­­­­­­­­­­­­­­­­---------------------
 * $LastChangedRevision: 128 $
 * $LastChangedBy: SD $
 * $LastChangedDate: 2009-06-23 12:33:08 +0200 (Tue, 23 Jun 2009) $
 * $HeadURL: svn://lvis10.fbm.fht-stuttgart.de/common/Common/src/de/hft_stuttgart/botwar/common/commands/GameOverCmd.java $
 ******************************************************************************/
package de.hft_stuttgart.botwar.common.commands;

import de.hft_stuttgart.botwar.common.models.GameResultInfo;
import java.util.Iterator;
import java.util.List;

/**
 * Is send to the player if the game is over
 * @author c.a.duevel@gmail.com, sdegiorgi@gmail.com, pmv-mail@gmx.de
 */
public class GameOverCmd extends Cmd{
    private final List<GameResultInfo> playerlist;

    /**
     * Returns the game over command.
     * @param playerList a list of all players ?
     */
    public GameOverCmd(final List<GameResultInfo> playerList) {
       this.playerlist = playerList;
    }

    /**
     * This returns only one Winner. To read all information use getPlayerList()
     * @return the userID of the winner
     */
    @Deprecated
    public final String getWinnerID() {
        return playerlist.get(0).getPlayerId();
    }

    /**
     * @return list of competitors.
     */
    public final List<GameResultInfo> getPlayerList() {
        return playerlist;
    }

    /**
     * Does the needed equals comparison
     * @param obj the object to compare with
     * @return a boolen representing the comparison result
     */
    @Override
    public final boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof GameOverCmd)) {
            return false;
        }

        final GameOverCmd other = (GameOverCmd) obj;
        if (this.getPlayerList().size() != other.getPlayerList().size()){
            return false;
        }

        Iterator<GameResultInfo> it1 = this.getPlayerList().iterator();
        Iterator<GameResultInfo> it2 = other.getPlayerList().iterator();
        while (it1.hasNext()){
            if (!it1.next().equals(it2.next())){
                return false;
            }
        }
        return true;
    }

    /**
     * Method to get a String representation of the object
     * @return String stating the Object
     */
    @Override
    public final String toString() {
        return String.format("GameOver: Winner is %s", getWinnerID());
    }
}
